//
//  XSQNetWorkTools.swift
//  WeiBo
//
//  Created by 许世强 on 2017/3/30.
//  Copyright © 2017年 许世强. All rights reserved.
//

import UIKit
import AFNetworking

//创建枚举- 网络请求类型
enum RequestType{
    //使用小写
    case get
    
    case post
    
}

//封装网络工具类
class XSQNetWorkTools: AFHTTPSessionManager {

    //单例的全局访问点
    static let sharedTools: XSQNetWorkTools = {
       
        let tools = XSQNetWorkTools()
        
        //追加text/html
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        
         tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    }()

    //网络请求方法
  fileprivate func request(type:RequestType,urlStr:String,params:Any?, finishCallBack:@escaping (Any?,Error?)->()){
        
        if type == .post{
            
            post(urlStr, parameters: params, progress: nil, success: { (_, response) in
                
                finishCallBack(response,nil)
                
                }, failure: { (_, error) in
                
                    
                finishCallBack(nil,error)
            })
        } else{
            
            get(urlStr, parameters: params, progress: nil, success: { (_, response) in
                
                finishCallBack(response,nil)
                
                }, failure: { (_, error) in
                    
                    finishCallBack(nil,error)
            })
            
        }
    }

}

//MARK: ----首页相关接口
extension XSQNetWorkTools{

    //发送access_token即可   finishCallBack为回调的数据
    func requestUserInfo(access_token:String,finishCallBack:@escaping (Any?,Error?)->()){
     
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        //请求参数 . 字典类型
        let params:[String:Any] = [
            
            //令牌
            "access_token":access_token
            
        ]
        
          request(type: .get, urlStr: urlStr, params: params ,finishCallBack: finishCallBack )
    }
}


//MARK: ----OAuth登录相关接口
extension XSQNetWorkTools {
    
    //通过access_token和用户id,获取用户信息
    func requestUserInfo(access_token:String, uid:Int64 ,finishCallBack:@escaping (Any?,Error?)->()){
    
        //请求地址
        let urlStr = "https://api.weibo.com/2/users/show.json"
        
        //请求参数 . 字典类型
        let params:[String:Any] = [
            
            //令牌
            "access_token":access_token,
            //用户id
            "uid":uid
        ]
        
           request(type: .get, urlStr: urlStr, params: params ,finishCallBack: finishCallBack )
    }
 
    //通过code获取access_token
    func requestAccessToken(code:String,finishCallBack:@escaping (Any?,Error?)->()){
    
        //请求地址
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        
        //请求参数 . 字典类型
        let params:[String:Any] = [
            "client_id":AppKey,
            "client_secret":AppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":Redirect_Uri
        ]
        
        //发送网络请求, 调用上面的网络请求方法  闭包的回调不能在这里,应该是在控制器XSQOAuthViewController中回调,逃逸闭包
        request(type: .post, urlStr: urlStr, params: params ,finishCallBack: finishCallBack )
        
    }
    
}


















